package com.niuniu.rushdorm.controller;



import com.niuniu.rushdorm.exception.ReportedException;
import com.niuniu.rushdorm.pojo.vo.UserVO;
import com.niuniu.rushdorm.pojo.vo.ResultVO;
import com.niuniu.rushdorm.service.GroupsService;

import com.niuniu.rushdorm.util.StatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping(value = {"/team"})
public class GroupsController {

    @Autowired
    private GroupsService groupsService;
    @PostMapping("/create")
    public ResultVO create(HttpServletRequest request,  @RequestBody Map<String, String> requestMap)
    {
        //获取uid
        long uid = (Long) request.getAttribute("uid");
        //获取请求体数据
        String name = requestMap.get("name");
        String desc = requestMap.get("describe");

        return groupsService.create(name, desc, uid);

    }

    @PostMapping("/del")
    public ResultVO delete(HttpServletRequest request)
    {

        //获取uid
        long uid = (Long) request.getAttribute("uid");

        return groupsService.delete(uid);

    }

    @PostMapping("/join")
    public ResultVO join(HttpServletRequest request, @RequestBody Map<String, String> requestMap)
    {
        UserVO  userVO = (UserVO)request.getAttribute("userVO");
        String inviteCode = requestMap.get("invite_code");
        return groupsService.join(userVO.getUid(), userVO.getGender(),  inviteCode);
    }

    @PostMapping("/quit")
    public ResultVO leave(HttpServletRequest request)
    {
        long uid = (Long)request.getAttribute("uid");
        return groupsService.leave(uid);
    }

    @GetMapping("/my")
    public ResultVO my(HttpServletRequest request)
    {
        long uid = (Long)request.getAttribute("uid");
        return groupsService.my(uid);
    }

    @PostMapping("/transfer")
    public ResultVO transfer(HttpServletRequest request, @RequestBody Map<String, String> requestMap)
    {
        String studentId = requestMap.get("student_id");
        long uid = (Long)request.getAttribute("uid");

        return groupsService.transfer(uid, studentId);
    }
}
